package com.xy.p2p.action.front;



import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.xy.common.page.Pagination;
import com.xy.common.util.DictUtils;
import com.xy.common.web.CookieUtils;
import com.xy.core.web.WebErrors;
import com.xy.p2p.model.Tmember;
import com.xy.p2p.model.Ttransferinfo;
import com.xy.p2p.model.Ttransferrecord;
import com.xy.p2p.service.TtransferrecordService;



@Controller
public class TtransferrecordAction{
	private static final Logger log = LoggerFactory.getLogger(TtransferrecordAction.class);
	public TtransferrecordAction() {
	}
 
	//购买债权,输入金额后提交操作
	@RequestMapping("/member/v_ttransferrecordPreSubmit.jspx")
	public String preSubmit(Ttransferrecord ttransferrecord, HttpServletRequest request, ModelMap model) {
//		Ttransferinfo ttransferinfo = ttransferinfoService.get(tiid);
//		ttransferinfo.setTrprice();
		ttransferrecord.setTmemberByTrmemberid((Tmember)request.getSession().getAttribute(TmemberAction.AUTH_KEY));
		model.addAttribute("ttransferrecord", ttransferrecordService.preSubmit(ttransferrecord));
        setDict(model);
		return "/front/businessPage/transferinfoeConfirm.html";
	}
	

	//确认购买债权
	@RequestMapping("/member/o_ttransferrecordSubmit.jspx")
	public String submit(Long tiid,double trmoney, HttpServletRequest request, ModelMap model) {
		Ttransferinfo ttransferinfo = new Ttransferinfo();
		ttransferinfo.setTiid(tiid);
		Ttransferrecord ttransferrecord = new Ttransferrecord();
		ttransferrecord.setTtransferinfo(ttransferinfo);
		ttransferrecord.setTrmoney(trmoney);
		ttransferrecord.setTmemberByAcmemberid((Tmember)request.getSession().getAttribute(TmemberAction.AUTH_KEY));
		if(null!=ttransferrecordService.save(ttransferrecord)){
	        setDict(model);
			return "redirect:/member/v_ttransferinfoBuyableList.jspx";
		}else{
			return "error";
		}
		
	}
	
	
 
 	/**
	 * 转让标的记录表新增页面
	 * 
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_ttransferrecordAdd.do")
	public String add(HttpServletRequest request, ModelMap model) {
		model.addAttribute("ttransferrecord", new Ttransferrecord());
        setDict(model);
		return "ttransferrecordEdit";
	}

	/**
	 * 转让标的记录表新增保存
	 * 
	 * @param ttransferrecord
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("/o_ttransferrecordSave.do")
	public String save(Ttransferrecord ttransferrecord, HttpServletRequest request, ModelMap model) {
		WebErrors errors = validateSave(ttransferrecord, request);
		if (errors.hasErrors()) {
			return errors.showErrorPage(model);
		}
		ttransferrecord = ttransferrecordService.save(ttransferrecord);
		return "redirect:v_ttransferrecordList.do";
	}

	/**
	 * 转让标的记录表删除
	 * 
	 * @param ids
	 * @return
	 */
	@RequestMapping("o_ttransferrecordDelete.do")
	public String delete(long[] ids) {
		if (ids != null) {
			for (long id : ids) {
				ttransferrecordService.remove(id);
			}
		}
		return "redirect:v_ttransferrecordList.do";
	}

	/**
	 * 转让标的记录表编辑页面
	 * 
	 * @param trid
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_ttransferrecordEdit.do")
	public String edit(long trid, HttpServletRequest request, ModelMap model) {
		Ttransferrecord ttransferrecord = ttransferrecordService.get(trid);
		model.addAttribute("ttransferrecord", ttransferrecord);
        setDict(model);
		return "ttransferrecordEdit";
	}

	/**
	 * 转让标的记录表详情
	 * 
	 * @param trid
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_ttransferrecordDetail.do")
	public String detail(long trid, HttpServletRequest request, ModelMap model) {
		Ttransferrecord ttransferrecord = ttransferrecordService.get(trid);
		model.addAttribute("ttransferrecord", ttransferrecord);
        setDict(model);
		return "ttransferrecordView";
	}

	/**
	 * 转让标的记录表查询（分页）
	 * 
	 * @param ttransferrecord
	 * @param pageNo
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_ttransferrecordList.do")
	public String listPage(Ttransferrecord ttransferrecord, Integer pageNo,
			HttpServletRequest request, ModelMap model) {
		if (pageNo == null)
			pageNo = 1;
		Pagination pagination = ttransferrecordService.getTtransferrecordByPage(toMap(ttransferrecord),
				pageNo, CookieUtils.getPageSize(request));
		model.addAttribute("pagination", pagination);
        setDict(model);
		return "ttransferrecordList";
	}

	/**
	 * 转让标的记录表查询
	 * 
	 * @param ttransferrecord
	 * @param request
	 * @param model
	 * @return
	 */
	@RequestMapping("v_ttransferrecordList1.do")
	public String list(Ttransferrecord ttransferrecord, HttpServletRequest request, ModelMap model) {
		List<Ttransferrecord> list = ttransferrecordService.getTtransferrecord(toMap(ttransferrecord));
		model.addAttribute("list", list);
        setDict(model);
		return "ttransferrecordList";
	}


 	/***
	 * 将页面输入信息转成map
	 * 
	 * @param ttransferrecord
	 * @return
	 */	private Map<String,Object> toMap(Ttransferrecord ttransferrecord){
		Map<String,Object> map=new HashMap<String, Object>();
      		if(ttransferrecord.getTrid()!=null&&!ttransferrecord.getTrid().equals("")){
			map.put("trid", ttransferrecord.getTrid());
		}
		if(ttransferrecord.getTtransferinfo()!=null){
			map.put("Ttransferinfo", ttransferrecord.getTtransferinfo());
		}
		if(ttransferrecord.getTmemberByTrmemberid()!=null){
			map.put("TmemberByTrmemberid", ttransferrecord.getTmemberByTrmemberid());
		}
		if(ttransferrecord.getTmemberByAcmemberid()!=null){
			map.put("TmemberByAcmemberid", ttransferrecord.getTmemberByAcmemberid());
		}
		if(ttransferrecord.getTrmoney()!=null&&!ttransferrecord.getTrmoney().equals("")){
			map.put("trmoney", ttransferrecord.getTrmoney());
		}
		if(ttransferrecord.getTrfactor()!=null&&!ttransferrecord.getTrfactor().equals("")){
			map.put("trfactor", ttransferrecord.getTrfactor());
		}
		if(ttransferrecord.getTrreason()!=null&&!ttransferrecord.getTrreason().equals("")){
			map.put("trreason", ttransferrecord.getTrreason());
		}
		if(ttransferrecord.getTrprice()!=null&&!ttransferrecord.getTrprice().equals("")){
			map.put("trprice", ttransferrecord.getTrprice());
		}
		if(ttransferrecord.getTrdatetime()!=null&&!ttransferrecord.getTrdatetime().equals("")){
			map.put("trdatetime", ttransferrecord.getTrdatetime());
		}      
		return map;
	}
   
	/**
	 * 设置页面所需的数据字典
	 * @param model
	 */
	private void setDict(ModelMap model){
	      model.addAttribute("statusMap", DictUtils.getDictByType(DictUtils.TTRANSFERINFO_STATUS));
	}

	/**
	 * 数据校验
	 * @param model
	 */
	private WebErrors validateSave(Ttransferrecord ttransferrecord, HttpServletRequest request) {
		WebErrors errors = WebErrors.create(request);
		/**if (errors.ifOutOfLength(username, "username", 1, 100)) {
			return errors;
		}  */    
		return errors;
	}

   
      
	@Resource
	private TtransferrecordService ttransferrecordService;   
}